package com.imddy.spbatch001.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;

public class RandomUtils {
    private static Logger logger = LoggerFactory.getLogger(RandomUtils.class);
    private static final String NumberStr = "0123456789";
    private static final String LowerAlphaStr = "abcdefghijklmnopqrstuvwxyz";
    private static final String UpperAlphaStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String AlphaStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String AlphaNumberStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /** 返回指定长度的随机数字字符串 */
    public static String getRandomNumber(int length) {
        return getRandom(length, NumberStr);
    }

    /** 返回指定长度的随机小写字母字符串 */
    public static String getRandomLowerAlpha(int length) {
        return getRandom(length, LowerAlphaStr);
    }

    /** 返回指定长度的随机大写字母字符串 */
    public static String getRandomUpperAlpha(int length) {
        return getRandom(length, UpperAlphaStr);
    }

    /** 返回指定长度的随机字母字符串 */
    public static String getRandomAlpha(int length) {
        return getRandom(length, AlphaStr);
    }

    /** 返回指定长度的随机数字字母字符串 */
    public static String getRandomAlphaNumber(int length) {
        return getRandom(length, AlphaNumberStr);
    }


    /** 根据提供的字符串，返回指定长度随机字符串 */
    public static String getRandom(int length, String source) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++ ) {
            sb.append(source.charAt(random.nextInt(source.length())));
        }
        return sb.toString();
    }


    public static void main(String[] args) {
        logger.info("字母数字: " + RandomUtils.getRandomAlphaNumber(15) );
        logger.info("仅仅数字: " + RandomUtils.getRandomNumber(15) );
        logger.info("小写字母: " + RandomUtils.getRandomLowerAlpha(15) );
        logger.info("大写字母: " + RandomUtils.getRandomUpperAlpha(15) );
        logger.info("仅仅字母: " + RandomUtils.getRandomAlpha(15) );

        logger.info("other: " + RandomUtils.getRandom(15,"而在这一过程中，他曾认真过思考究竟什么样的群集大小才能为将来的 Windows 用户提供潜在的支持。然后发现，庞大的群集数量会带来同样庞大的容量，但也会造成巨大的空间占用。譬如：选择 32kb 的群集大小，即使是最小的 Hello, World 程序所需的几个字节，也会占用 32kb 甚至更多的磁盘空间") );
    }


}
